package net.xdclass.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.manager.ShortLinkManager;
import net.xdclass.mapper.ShortLinkMapper;
import net.xdclass.model.ShortLinkDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 功能描述
 *
 * @author ASUS
 * @date 2024/07/28  14:17
 */
@Component
@Slf4j
public class ShortLinkManagerImpl implements ShortLinkManager {
    @Autowired
    private ShortLinkMapper shortLinkMapper;
    @Override
    public int addShortLink(ShortLinkDO shortLinkDO)
    {
        return shortLinkMapper.insert(shortLinkDO);
    }
    @Override
    public ShortLinkDO findByShortLinkCode(String shortLinkCode) {
        ShortLinkDO shortLinkDO = shortLinkMapper.selectOne(new QueryWrapper<ShortLinkDO>().eq("code",
                        shortLinkCode).eq("del", 0));
        return shortLinkDO;
    }
    /**
     * 逻辑删除
     *
     * @param shortLinkCode
     * @param accountNo
     * @return
     */
    @Override
    public int del(String shortLinkCode, Long accountNo) {
        ShortLinkDO shortLinkDO = new ShortLinkDO();
        shortLinkDO.setDel(1);
        //第一个参数 shortLinkDO 包含了你希望更新的字段及其新值，第二个参数 QueryWrapper 用于指定筛选条件。
        int rows = shortLinkMapper.update(shortLinkDO, new QueryWrapper<ShortLinkDO>()
                        .eq("code", shortLinkCode).eq("account_no", accountNo));
        return rows;
    }

    @Override
    public int update(ShortLinkDO shortLinkDO) {
//        int rows = shortLinkMapper.update(null, new UpdateWrapper<ShortLinkDO>()
//                .eq("code", shortLinkDO.getCode())
//                .eq("del", 0)
//                .eq("account_no",shortLinkDO.getAccountNo())
//
//                .set("title", shortLinkDO.getTitle())
//                .set("domain", shortLinkDO.getDomain()));
//
//        LambdaQueryWrapper<ShortLinkDO> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(ShortLinkDO::getCode,shortLinkDO.getCode())
//                .eq(ShortLinkDO::getDel,0)
//                .eq(ShortLinkDO::getAccountNo,shortLinkDO.getAccountNo());
//        ShortLinkDO newShortLinkDO = shortLinkMapper.selectOne(wrapper);
//        newShortLinkDO.setTitle(shortLinkDO.getTitle());
//        newShortLinkDO.setDomain(shortLinkDO.getDomain());
//        int rows = shortLinkMapper.updateById(newShortLinkDO);
//        System.out.println(rows);
        LambdaUpdateWrapper<ShortLinkDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ShortLinkDO::getCode,shortLinkDO.getCode())
                .eq(ShortLinkDO::getDel,0)
                .eq(ShortLinkDO::getAccountNo,shortLinkDO.getAccountNo())
                .set(ShortLinkDO::getTitle,shortLinkDO.getTitle())
                .set(ShortLinkDO::getDomain,shortLinkDO.getDomain());
        int rows = shortLinkMapper.update(null, wrapper);

        return rows;
    }


}

